package com.xie.leetcode.array;

//786. 第 K 个最小的素数分数
//        给你一个按递增顺序排序的数组 arr 和一个整数 k 。数组 arr 由 1 和若干 素数  组成，且其中所有整数互不相同。
//
//        对于每对满足 0 < i < j < arr.length 的 i 和 j ，可以得到分数 arr[i] / arr[j] 。
//
//        那么第 k 个最小的分数是多少呢?  以长度为 2 的整数数组返回你的答案, 这里 answer[0] == arr[i] 且 answer[1] == arr[j] 。
//
//
//        示例 1：
//
//        输入：arr = [1,2,3,5], k = 3
//        输出：[2,5]
//        解释：已构造好的分数,排序后如下所示:
//        1/5, 1/3, 2/5, 1/2, 3/5, 2/3
//        很明显第三个最小的分数是 2/5
//        示例 2：
//
//        输入：arr = [1,7], k = 1
//        输出：[1,7]
//
//
//        提示：
//
//        2 <= arr.length <= 1000
//        1 <= arr[i] <= 3 * 104
//        arr[0] == 1
//        arr[i] 是一个 素数 ，i > 0
//        arr 中的所有数字 互不相同 ，且按 严格递增 排序
//        1 <= k <= arr.length * (arr.length - 1) / 2

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiezhendong
 * @date 2021/11/29
 */
public class KthSmallestPrimeFraction {
    public static void main(String[] args) {
        KthSmallestPrimeFraction kthSmallestPrimeFraction = new KthSmallestPrimeFraction();
        System.out.println(Arrays.toString(kthSmallestPrimeFraction.kthSmallestPrimeFraction(new int[]{1, 2, 3, 5}, 3)));
        System.out.println(Arrays.toString(kthSmallestPrimeFraction.kthSmallestPrimeFraction(new int[]{1, 7}, 1)));
        System.out.println(Arrays.toString(kthSmallestPrimeFraction.kthSmallestPrimeFraction(new int[]{1, 5, 7, 11, 19, 41, 59, 79, 89, 101, 107, 127, 157, 197, 199, 239, 241, 257, 269, 277, 293, 307, 311, 337, 373, 379, 397, 409, 431, 457, 467, 491, 509, 521, 523, 563, 599, 631, 659, 757, 797, 829, 863, 877, 887, 947, 967, 1033, 1039, 1049, 1069, 1091, 1109, 1117, 1123, 1163, 1213, 1217, 1223, 1231, 1237, 1249, 1259, 1289, 1291, 1297, 1303, 1373, 1381, 1399, 1439, 1447, 1453, 1481, 1559, 1571, 1601, 1607, 1613, 1667, 1699, 1747, 1759, 1811, 1873, 1879, 1889, 1933, 1951, 1987, 2011, 2017, 2029, 2053, 2063, 2069, 2089, 2111, 2129, 2141, 2161, 2237, 2273, 2311, 2333, 2339, 2341, 2357, 2381, 2399, 2411, 2531, 2591, 2593, 2729, 2741, 2777, 2843, 2857, 2861, 2879, 2887, 2927, 3001, 3061, 3109, 3163, 3203, 3221, 3229, 3251, 3299, 3301, 3313, 3359, 3407, 3467, 3527, 3541, 3557, 3581, 3637, 3659, 3671, 3739, 3769, 3797, 3847, 3851, 3877, 3907, 3911, 3923, 3931, 4021, 4057, 4127, 4139, 4177, 4201, 4211, 4229, 4243, 4289, 4357, 4363, 4391, 4397, 4409, 4457, 4483, 4513, 4517, 4523, 4547, 4549, 4567, 4597, 4637, 4651, 4691, 4703, 4783, 4799, 4889, 4933, 4937, 4943, 4951, 4957, 4967, 5003, 5023, 5051, 5059, 5099, 5101, 5147, 5167, 5179, 5189, 5197, 5237, 5279, 5281, 5333, 5387, 5441, 5449, 5471, 5501, 5503, 5557, 5569, 5623, 5639, 5641, 5653, 5659, 5683, 5711, 5741, 5783, 5791, 5849, 5867, 5881, 5923, 5981, 6011, 6043, 6053, 6073, 6079, 6089, 6113, 6131, 6133, 6151, 6163, 6173, 6199, 6203, 6257, 6287, 6311, 6317, 6323, 6337, 6343, 6361, 6367, 6379, 6451, 6473, 6529, 6551, 6571, 6581, 6673, 6689, 6709, 6781, 6803, 6823, 6829, 6833, 6841, 6871, 6899, 6917, 6959, 6991, 7027, 7043, 7057, 7207, 7211, 7213, 7229, 7253, 7283, 7297, 7331, 7333, 7417, 7451, 7481, 7499, 7517, 7523, 7529, 7537, 7547, 7559, 7583, 7589, 7591, 7639, 7673, 7681, 7727, 7789, 7793, 7817, 7823, 7853, 7873, 7883, 7901, 7919, 7951, 8009, 8011, 8053, 8069, 8081, 8167, 8179, 8219, 8221, 8269, 8311, 8329, 8363, 8527, 8599, 8641, 8647, 8689, 8719, 8779, 8783, 8803, 8821, 8831, 8849, 8867, 8887, 8893, 8923, 8963, 8969, 9001, 9109, 9199, 9221, 9239, 9241, 9293, 9311, 9337, 9377, 9397, 9433, 9461, 9473, 9551, 9587, 9623, 9649, 9677, 9721, 9743, 9749, 9769, 9791, 9811, 9817, 9829, 9833, 9857, 9929, 10007, 10037, 10061, 10091, 10093, 10151, 10159, 10253, 10259, 10343, 10391, 10513, 10559, 10631, 10657, 10711, 10753, 10799, 10859, 10903, 10957, 10987, 10993, 11083, 11113, 11119, 11131, 11171, 11173, 11177, 11197, 11243, 11251, 11317, 11329, 11351, 11393, 11411, 11437, 11447, 11467, 11489, 11497, 11519, 11551, 11587, 11597, 11617, 11677, 11717, 11719, 11743, 11783, 11827, 11831, 11833, 11839, 11863, 11897, 11903, 11927, 11941, 11981, 12071, 12097, 12101, 12109, 12113, 12143, 12157, 12203, 12239, 12251, 12253, 12277, 12323, 12329, 12373, 12377, 12379, 12451, 12491, 12517, 12547, 12613, 12703, 12743, 12757, 12763, 12781, 12959, 13001, 13003, 13043, 13093, 13159, 13219, 13267, 13297, 13339, 13367, 13381, 13411, 13469, 13513, 13553, 13691, 13709, 13723, 13729, 13781, 13789, 13829, 13877, 13933, 13967, 13999, 14011, 14087, 14243, 14251, 14327, 14341, 14347, 14369, 14401, 14431, 14519, 14543, 14561, 14591, 14653, 14669, 14683, 14699, 14737, 14741, 14753, 14759, 14767, 14771, 14779, 14797, 14821, 14843, 14867, 14869, 14887, 14891, 14897, 14923, 14951, 15031, 15073, 15121, 15173, 15227, 15233, 15263, 15277, 15299, 15319, 15329, 15331, 15359, 15361, 15373, 15401, 15439, 15497, 15559, 15569, 15581, 15629, 15671, 15727, 15791, 15817, 15859, 15877, 15887, 15889, 15901, 15937, 15991, 16063, 16091, 16139, 16187, 16193, 16217, 16229, 16267, 16319, 16333, 16361, 16363, 16447, 16487, 16493, 16519, 16547, 16553, 16573, 16603, 16631, 16649, 16651, 16657, 16693, 16741, 16763, 16883, 16889, 16921, 16931, 16943, 16963, 16981, 16987, 17029, 17033, 17077, 17107, 17117, 17123, 17203, 17209, 17231, 17239, 17317, 17321, 17327, 17341, 17351, 17377, 17383, 17443, 17483, 17489, 17509, 17551, 17569, 17579, 17623, 17657, 17659, 17707, 17749, 17761, 17789, 17791, 17837, 17881, 17891, 17909, 17911, 17923, 18047, 18049, 18061, 18077, 18097, 18119, 18121, 18131, 18143, 18169, 18181, 18199, 18229, 18301, 18307, 18311, 18341, 18353, 18367, 18371, 18427, 18433, 18583, 18593, 18661, 18671, 18679, 18731, 18743, 18749, 19031, 19037, 19079, 19139, 19157, 19163, 19207, 19237, 19267, 19309, 19319, 19379, 19391, 19423, 19427, 19429, 19447, 19457, 19463, 19531, 19543, 19553, 19559, 19577, 19687, 19697, 19739, 19751, 19753, 19759, 19763, 19793, 19801, 19843, 19867, 19889, 19927, 19979, 19991, 19993, 19997, 20021, 20047, 20089, 20101, 20117, 20123, 20173, 20233, 20249, 20327, 20341, 20353, 20389, 20407, 20431, 20441, 20483, 20533, 20639, 20707, 20731, 20753, 20789, 20809, 20849, 20959, 20963, 21001, 21019, 21031, 21089, 21101, 21143, 21269, 21277, 21313, 21319, 21377, 21383, 21397, 21407, 21467, 21521, 21523, 21563, 21569, 21577, 21611, 21649, 21661, 21673, 21683, 21701, 21773, 21799, 21817, 21821, 21839, 21851, 21911, 21997, 22027, 22037, 22039, 22091, 22147, 22153, 22157, 22159, 22171, 22193, 22273, 22291, 22367, 22381, 22391, 22397, 22481, 22511, 22531, 22541, 22549, 22573, 22651, 22697, 22721, 22783, 22787, 22811, 22817, 22859, 22861, 22871, 22901, 22963, 23021, 23053, 23143, 23203, 23209, 23251, 23293, 23297, 23311, 23321, 23431, 23473, 23537, 23581, 23633, 23677, 23687, 23743, 23761, 23789, 23827, 23831, 23833, 23899, 23911, 23917, 23971, 23981, 23993, 24007, 24043, 24049, 24061, 24071, 24137, 24223, 24229, 24371, 24413, 24439, 24443, 24469, 24481, 24499, 24527, 24593, 24631, 24659, 24683, 24697, 24709, 24763, 24767, 24799, 24979, 25033, 25057, 25121, 25153, 25163, 25171, 25183, 25229, 25243, 25253, 25303, 25321, 25367, 25409, 25423, 25447, 25457, 25537, 25541, 25561, 25583, 25633, 25657, 25673, 25693, 25733, 25793, 25867, 25913, 25919, 25951, 25981, 26017, 26161, 26171, 26203, 26209, 26237, 26251, 26267, 26357, 26459, 26501, 26513, 26561, 26627, 26633, 26669, 26711, 26713, 26717, 26723, 26777, 26839, 26893, 26947, 26951, 26987, 26993, 27011, 27017, 27043, 27061, 27091, 27107, 27127, 27143, 27253, 27281, 27361, 27367, 27409, 27427, 27431, 27457, 27487, 27529, 27611, 27631, 27653, 27673, 27733, 27737, 27791, 27793, 27803, 27809, 27817, 27827, 27847, 27893, 27953, 27961, 28081, 28097, 28151, 28183, 28219, 28277, 28319, 28403, 28477, 28493, 28513, 28517, 28537, 28607, 28619, 28643, 28669, 28697, 28751, 28771, 28789, 28793, 28813, 28843, 28859, 28867, 28901, 28949, 29027, 29033, 29059, 29137, 29167, 29207, 29243, 29327, 29339, 29347, 29363, 29389, 29501, 29527, 29567, 29573, 29581, 29587, 29599, 29663, 29723, 29741, 29759, 29803, 29863, 29873, 29881}, 207553)));
    }

    public int[] kthSmallestPrimeFraction(int[] arr, int k) {
        List<int[]> valueList = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                int[] value = new int[]{arr[i], arr[j]};
                valueList.add(value);
            }
        }

        List<int[]> newValueList = valueList.stream().sorted(
                (x, y) -> x[0] * y[1] - y[0] * x[1]
        ).collect(Collectors.toList());

        return newValueList.get(k - 1);
    }
}
